package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.CollectionBehaviorDto;
import com.heima.behavior.dtos.LikesBehaviorDto;
import com.heima.behavior.pojos.ApCollectionBehavior;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.common.apuser.pojos.ApUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.threadLocal.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 用户收藏行为
     *
     * @param dto
     */
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //1.校验参数
        if (dto.getEntryId() == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取登录用户id
        ApUser apUser = ApThreadLocalUtil.getUser();
        //表示未登录
        if(apUser==null){
            return ResponseResult.okResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //游客身份不能进行以下操作
        if(apUser.getId()==0){
            return ResponseResult.okResult(405,"游客身份禁止，需进行登录");
        }

        //3. 查询当前用户是否已经收藏
        //定义redis的key
        String collection_key="USER_COLLECTION_"+dto.getEntryId();
        Boolean flag = redisTemplate.opsForHash().hasKey(collection_key, apUser.getId()+"");

        //4.如果已经收藏，并且正在执行收藏动作，直接返回
        if(flag && dto.getOperation()==0){
            return ResponseResult.okResult(AppHttpCodeEnum.COLLECTION_USER_END_SUCCESS);
        }

        //5.如果用户已经收藏，但是正在执行的是取消收藏，则删除redis和mongo
        if(flag && dto.getOperation()==1){
            //删除redis
            redisTemplate.opsForHash().delete(collection_key,apUser.getId()+"");
            //删除mongo
            mongoTemplate.remove(
                    Query.query(Criteria.where("userId").is(apUser.getId()+"")
                            .and("articleId").is(dto.getEntryId()))
                    , ApCollectionBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.COLLECTION_USER_CANCEL_SUCCESS);
        }

        //6.如果用户没有点收藏，则正在执行的是收藏动作，则添加redis和mongo
        if(!flag && dto.getOperation()==0){
            //添加redis
            redisTemplate.opsForHash().put(collection_key,apUser.getId()+"","1");
            //添加mongo
            ApCollectionBehavior apCollectionBehavior=new ApCollectionBehavior();
            apCollectionBehavior.setId(ObjectId.get().toHexString());
            apCollectionBehavior.setEntryId(dto.getEntryId());
            apCollectionBehavior.setCollectionTime(dto.getCreateTime());
            apCollectionBehavior.setUserId(apUser.getId().longValue());
            apCollectionBehavior.setType((short) 0);//默认是文章收藏
            mongoTemplate.insert(apCollectionBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.COLLECTION_USER_SUCCESS);
        }
        //7.返回数据
        return null;
    }
}